//
//  UITextField.swift
//  Adorbee
//
//  Created by amovision on 2024/11/28.
//

import UIKit

public struct YHTextFieldItem {
  public enum Style {
    case padding, title, image, view
  }
  
  public let content: UIView
  public let style: Style
  
  public static func padding(_ value: CGFloat = 8) -> Self {
    let size = CGSizeMake(value, 10)
    let content = UIView(frame: size.rect)
    return Self(content: content, style: .title)
  }
  
  public static func title(_ title: Any, maxWidth: CGFloat = .infinity) -> Self {
    let title = AString(title, font: .num(16), color: .label)
    let size = CGSizeMake(min(title.width + 16, maxWidth), 50)
    let button = UIButton(title: title)
    button.titleLabel?.numberOfLines = 0
    button.titleLabel?.minimumScaleFactor = 0.7
    button.titleLabel?.adjustsFontSizeToFitWidth = true
    let content = UIView(frame: size.rect)
    button.frame = content.bounds
    content.addSubview(button)
    return Self(content: content, style: .title)
  }
  public static func image(_ image: Any, size: CGSize = .zero) -> Self {
    let img = UIImage.named(image)
    let button = UIButton(image: img)
    let sz = size == .zero ? (img?.size ?? [50].size) : size
    let content = UIView(frame: sz.rect)
    button.frame = content.bounds
    content.addSubview(button)
    return Self(content: content, style: .image)
  }
  public static func view(_ view: UIView, size: CGSize = .zero) -> Self {
    let sz = size != .zero ? size : view.frame.size
    let content: UIView
    if view.isMember(of: UIView.self) {
      content = view
      content.frame = sz.rect
    } else {
      content = UIView(frame: sz.rect)
      view.frame = content.bounds
      content.addSubview(view)
    }
    return Self(content: content, style: .view)
  }
}

public extension UITextField {
  /// 配合`bk_shouldChangeCharactersInRangeWithReplacementStringBlock`使用
  func replacement(_ range: NSRange, _ string: String) -> String {
    if string.isEmpty {
      return ((text ?? "") as NSString).replacingCharacters(in: range, with: "")
    } else {
      var txt = text ?? ""
      txt.insert(contentsOf: string, at: txt.index(txt.startIndex, offsetBy: range.location))
      return txt
    }
  }

  private func setContent(_ content: YHTextFieldItem?, isLeft: Bool) {
    if isLeft {
      leftView = content?.content
      leftViewMode = .always
    } else {
      rightView = content?.content
      rightViewMode = .always
    }
  }

  func setRight(_ content: YHTextFieldItem?) {
    setContent(content, isLeft: false)
  }

  func setLeft(_ content: YHTextFieldItem?) {
    setContent(content, isLeft: true)
  }

  var leftButton: UIButton? {
    if let btn = leftView as? UIButton { btn }
    else if let btn = leftView?.subviews.first as? UIButton { btn }
    else { nil }
  }

  var rightButton: UIButton? {
    if let btn = rightView as? UIButton { btn }
    else if let btn = rightView?.subviews.first as? UIButton { btn }
    else { nil }
  }

  convenience init(left: YHTextFieldItem? = nil, right: YHTextFieldItem? = nil, hint: Any, font: UIFont, color: UIColor = .label, bColor: UIColor = .white, radius: CGFloat = 0) {
    self.init()
    autocorrectionType = .no
    spellCheckingType = .no
    smartQuotesType = .no
    smartDashesType = .no
    smartInsertDeleteType = .no
    textContentType = .oneTimeCode
    tintColor = .label
    clearButtonMode = .whileEditing
    setLeft(left)
    setRight(right)
    if let h = hint as? String {
      attributedPlaceholder = AString(h, font: .num(15), color: .placeholderText)
    } else if let h = hint as? AString {
      attributedPlaceholder = h
    }
    self.font = font
    self.textColor = color
    self.backgroundColor = bColor
    if radius > 0 { addCorner(radius) }
  }
}
